import axios from 'axios'
import qs from 'qs'
// import { MessageBox,Loading } from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css'
axios.defaults.timeout = 30000;                        //响应时间
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';           //配置请求头

let loadingInstance = null;

//请求拦截
axios.interceptors.request.use((config)=>{
  return config
})

axios.interceptors.response.use((response) => {
  // 以服务的方式调用的 Loading 需要异步关闭
  if(loadingInstance){loadingInstance.close();}
  
  return response
});



export function fetch(url, params,type,str) {
  return new Promise((resolve, reject) => {
    axios.defaults.baseURL = `https://${str}.labifenqi.com/`;

    if(type=='post'){
      axios.post(url, params)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err);
      })
      .catch((error) => {
        reject(error)
      })
    } else if(type == 'get'){
      axios.get(url,{params})
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err);
      })
      .catch((error) => {
        reject(error)
      })
    }
  })
}

